<?php function printGroupRow($view,$quantity,$quantity_after_change,$max,$min,$flag_product_code){?>
<tr class='row1'>
	<td colspan='3'></td>		
	<td class='right is_code'>
		<?php echo $quantity_after_change; ?>
	</td>	
	<td colspan='6'></td>
	
	<?php if($view->inventory_lot_code=="" && $view->unit_code!=''){?>
	<td class='right is_code'>					
		<?php echo $min; ?>				
	</td>
	<td class='right is_code'>
		<?php echo $max; ?>					
	</td>
	<td class='is_code'>
		<?php 
		$alert="";
		if($quantity_after_change<$min && !is_string($min))
		{
			$alert="Tồn kho < Tồn kho min ($quantity_after_change < $min)";
		}
		else if($quantity_after_change>$max && !is_string($max))
		{
			$alert="Tồn kho > Tồn kho max ($quantity_after_change > $max)";
		}
		if($alert!="")
		{
		 	$alert="Mã sản phẩm: $flag_product_code<br>".$alert;
		}
		echo $alert;
		?>
	</td>
	<?php }?>
</tr>	
<?php }?>
<?php $style_th = 'style="background-color:#CCCCCC"' ;?>
<?php $style_td = 'style="border:solid 1px #000"' ;?>
<?php $style_is_code = 'style="color:#FF0000"' ;?>
<?php $style_h2= 'style="width:100%;text-align:center;font-weight:bold;font-size:24px"';?>
<?php $style_description= ' style="width:100%;text-align:center;font-style:italic" ';?>

<div <?php echo $style_h2 ?>>BÁO CÁO TỒN KHO</div>
<br/>
<table cellpadding="4" cellspacing="0" border='1' style="width:100%; border-collapse:collapse">
	<thead>
		<tr>
			<th <?php echo $style_th ?>>STT</th>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Mã hàng hóa','column'=>'inventory_product_code')); ?>
			</th>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Tên hàng hóa','column'=>'product_name')); ?>
			</th>
			<?php if($view->unit_code!=''){?>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'SL sau khi<br/>qui đổi','column'=>'inventory_quantity_after_change')); ?>
			</th>		
			<?php }?>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Số lượng<br/>trong kho','column'=>'inventory_quantity')); ?>
			</th>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Đơn vị tính<br/>trong kho','column'=>'inventory_product_unit_code')); ?>
			</th>	
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Vị trí kho','column'=>'location_name')); ?>
			</th>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Ngày sản xuất','column'=>'inventory_product_time')); ?>
			</th>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Ngày hết hạn','column'=>'inventory_expire_time')); ?>
			</th>
			<th <?php echo $style_th ?>>
				<?php echo $this->partial('index/manage/caption.phtml','admin',array('caption'=>'Mã lot','column'=>'inventory_lot_code')); ?>
			</th>
			<?php if($this->inventory_lot_code=="" && $this->unit_code!=''){?>
			<th <?php echo $style_th ?>>
				Tồn kho min
			</th>
			<th <?php echo $style_th ?>>
				Tồn kho max
			</th>
			<th <?php echo $style_th ?>>
				Cảnh báo
			</th>
			<?php }?>
		</tr>
	</thead>
	<tbody>
	<?php if (count($this->paginator) > 0): $no=1;$flag_product_code="";?>
	<?php foreach ($this->paginator as $row): ?>			
		<?php 
		if ($flag_product_code != $inventory->inventory_product_code)  
		{
			if($flag_product_code!="" && $this->unit_code!="")
				printGroupRow($this,$quantity,$quantity_after_change,$max,$min,$flag_product_code);
			$min=$row->m_Array['product_instore_min'];
			$max=$row->m_Array['product_instore_max'];
			$flag_product_code=$row->inventory_product_code; 
			$quantity=0;
			$quantity_after_change=0;
		}
		$quantity += $inventory->inventory_quantity; 
		$quantity_after_change+=$row->m_Array['inventory_quantity_after_change'];		
		?>
		<tr>
			<td align="center"><?php echo $no++ ;?></td>
			<td>
				<?php echo $row->inventory_product_code; ?>
			</td>
			<td class='left'>
				<?php echo $row->m_Array['product_name']; ?>
			</td>
			<?php if($view->unit_code!=''){?>
			<td class='right'>
				<?php if($row->m_Array['inventory_quantity_after_change']>0 || $row->inventory_quantity==0){?>
					<?php echo $row->m_Array['inventory_quantity_after_change']; ?>
				<?php }else{?>
					<?php echo "-";?>
				<?php }?>
			</td>				
			<?php }?>
			<td class='right'>
				<?php echo $row->inventory_quantity; ?>
			</td>
			<td class='left'>
				<?php echo $row->inventory_product_unit_code; ?>
			</td>
			<td class='left'>
				<?php echo $row->m_Array['location_name']; ?>
			</td>				
			<td class='center'>
				<?php echo $this->partial('index/format/time.phtml','admin',array($row->inventory_product_time)); ?>
			</td>
			<td class='center'>
				<?php echo $this->partial('index/format/time.phtml','admin',array($row->inventory_expire_time)); ?>
			</td>
			<td class='left'>
				<?php echo $row->inventory_lot_code; ?>
			</td>
			<?php if($this->inventory_lot_code=="" && $this->unit_code!=''){?>
			<td></td>
			<td></td>
			<td></td>
			<?php }?>
		</tr>
	<?php endforeach;?> 
	<?php if($flag_product_code!="" && $this->unit_code!=""){printGroupRow($this,$quantity,$quantity_after_change,$max,$min,$flag_product_code);}?>		
	<?php endif; ?>
	</tbody>
</table>



